﻿using MongoDB.Driver;

namespace ZYHand.Repository
{
    public static class MongodbClient<T> where T : class
    {
        private static MongoClient client = null;

        #region +MongodbInfoClient 获取mongodb实例
        /// <summary>
        /// 获取mongodb实例
        /// </summary>
        /// <param name="host">连接字符串，库，表</param>
        /// <returns></returns>
        public static IMongoCollection<T> MongodbInfoClient(MongodbHost host)
        {
            if (client == null)
            {
                client = new MongoClient(host.Connection);
            }
            //  MongoClient client = new MongoClient(host.Connection);
            var dataBase = client.GetDatabase(host.DataBase);

            return dataBase.GetCollection<T>(host.Table);
        }


        /// <summary>
        /// object测试用方法
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        public static IMongoCollection<object> MongodbInfoClient2(MongodbHost host)
        {
            MongoClient client = new MongoClient(host.Connection);
            var dataBase = client.GetDatabase(host.DataBase);
            return dataBase.GetCollection<object>(host.Table);
        }
        #endregion
    }

}
